package com.daishuaiqing.blog.controller;

import com.daishuaiqing.blog.entity.ArticleType;
import com.daishuaiqing.blog.model.MyResult;
import com.daishuaiqing.blog.model.SuccessResult;
import com.daishuaiqing.blog.model.ValidFailResult;
import com.daishuaiqing.blog.service.ArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class ArticleTypeController {
    @Autowired
    ArticleTypeService articleTypeService;

    /**
     * 分类列表
     * @return
     */
    @GetMapping("/admin/article/type/list")
    public MyResult getList(){
        return articleTypeService.getList();
    }

    /**
     *
     * @param articleType
     * @param bindingResult
     * @return
     */
    @PostMapping("/admin/article/type/add")
    public MyResult add(@RequestBody @Valid ArticleType articleType,  BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return new ValidFailResult(bindingResult.getFieldError().getDefaultMessage());
        }else {
            return articleTypeService.add(articleType);
        }
    }

}
